package com.osmino.contacts.list;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.osmino.contacts.entity.Group;

public final class ArrayGroupsAdapter extends BaseAdapter {

	private final Context mContext;
	private List<Group> mListGroups;

	public ArrayGroupsAdapter(Context context) {
		mContext = context;
		mListGroups = new ArrayList<Group>();
	}

	@Override
	public int getCount() {
		return mListGroups.size();
	}

	@Override
	public Group getItem(int position) {
		return mListGroups.get(position);
	}

	@Override
	public long getItemId(int position) {
		return getItem(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView item = null;
		if (convertView == null) {
			item = (TextView) LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_multiple_choice,
					parent, false);
			item.setTextColor(Color.BLACK);
		} else {
			item = (TextView) convertView;
		}
		item.setText(getItem(position).title);
		return item;
	}

	public void setListGroups(List<Group> listGroups) {
		mListGroups = new ArrayList<Group>(listGroups);
		int size = mListGroups.size();
		for (int i = 0; i < size; i++) {
			Group group = mListGroups.get(i);
			if (group.id == Group.NO_GROUP) {
				mListGroups.remove(i);
				break;
			}
		}
		notifyDataSetChanged();
	}

}
